package com.webull.information.center.carwler.common.util.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.github.stuxuhai.jpinyin.ChineseHelper;
import com.webull.framework.util.UtilDate;
import com.webull.information.center.carwler.common.model.NewsInformation;
import com.webull.information.center.carwler.common.util.jsoup.HtmlBodyPrase;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;

/**
 * 新浪个股新闻资讯列表解析工具
 * 
 * @author shimingjun
 * @date 2016年8月26日 下午3:51:37
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class SinaNewsListUitl {
	private @Autowired
    HttpJsoupClient htmlPrase;
	protected final Logger logger = LogManager.getLogger(getClass());

	/**
	 * 获取新浪个股资讯首页对象并解析新闻条目列表
	 * 
	 * @param url
	 * @return
	 * @since 0.1.0
	 */
	public List<SinaItem> parseNewsList(String url, Integer regionId) {

		Document doc = htmlPrase.parseDocumentFromUrl(url);
		if (doc == null) {
			logger.warn("SinaNewsListUitl parseNewsList parse url:{} ,is null!!", url);
			return null;
		}
		if (regionId == 1) {
			return parseNewsList(doc);
		} else if (regionId == 2) {
			return parseNewsListHK(doc);
		} else if (regionId == 6) {
			return parseNewsListUS(doc);
		}
		return null;
	}

	private List<SinaItem> parseNewsListUS(Document doc) {
		List<SinaItem> result = new ArrayList<SinaItem>();

		// 美股新闻页面
		// Optional.ofNullable(doc.select("div.xb_news>ul.xb_list").last()).map(las0 ->
		// las0.select("li"))
		Optional.ofNullable(doc.select("div.xb_news>ul.xb_list>li"))
				.ifPresent(eles -> {
					for (int i = 0; eles != null && i < eles.size(); i++) {
						Element ele = eles.get(i);
						Optional.ofNullable(ele.select("span.xb_cred").first()).ifPresent(sss -> sss.remove());
						Optional.ofNullable(ele.select("a").first()).ifPresent(a0 -> {
							SinaItem opt = new SinaItem();

							opt.setLink(StringUtils.stripToNull(a0.attr("abs:href")));
							opt.setTitle(StringUtils.stripToNull(StringEscapeUtils.unescapeHtml4(a0.ownText())));
							Optional.ofNullable(ele.select("span.xb_list_r").first())
									.map(span0 -> StringUtils
											.deleteWhitespace(StringUtils.substringAfter(span0.ownText(), "|")))
									.ifPresent(str0 -> {
										// | 2016年08月29日 08:50
										opt.setNewsTimeStr(str0);
										try {
											opt.setNewsTime(
													UtilDate.parse(str0, Locale.CHINESE, 8, "yyyy年MM月dd日HH:mm"));
										} catch (Exception e) {
										}
									});

							if (StringUtils.isNotBlank(opt.getLink()) //
									&& opt.getNewsTime() != null //
									&& StringUtils.isNotBlank(opt.getTitle())
									&& ChineseHelper.containsChinese(opt.getTitle())) {
								result.add(opt);
							}
						});
					}
				});
		return result;
	}

	private List<SinaItem> parseNewsListHK(Document doc) {
		List<SinaItem> result = new ArrayList<SinaItem>();

		// 港股新闻页面
		Optional.ofNullable(doc.select("div.part02>ul.list01>li")).ifPresent(eles -> {

			for (int i = 0; eles != null && i < eles.size(); i++) {
				Element ele = eles.get(i);
				Optional.ofNullable(ele.select("a").first()).ifPresent(a0 -> {
					SinaItem opt = new SinaItem();

					opt.setLink(StringUtils.stripToNull(a0.attr("abs:href")));
					opt.setTitle(StringUtils.stripToNull(StringEscapeUtils.unescapeHtml4(a0.ownText())));
					Optional.ofNullable(a0.nextElementSibling()).map(span0 -> StringUtils.stripToNull(span0.ownText()))//
							.ifPresent(str -> {
								// 2016-08-17 17:20:00
								opt.setNewsTimeStr(str);
								try {
									opt.setNewsTime(UtilDate.parse(str, 8, "yyyy-MM-dd HH:mm"));
								} catch (Exception e) {
								}
							});

					if (StringUtils.isNotBlank(opt.getLink()) //
							&& opt.getNewsTime() != null //
							&& StringUtils.isNotBlank(opt.getTitle())) {
						result.add(opt);
					}
				});
			}
		});
		return result;
	}

	private List<SinaItem> parseNewsList(Document doc) {
		List<SinaItem> result = new ArrayList<SinaItem>();
		// A股新闻页面
		Optional.ofNullable(doc.select("div.datelist").first()).map(list0 -> list0.select("ul").first())
				.ifPresent(ul1 -> {
					List<Element> eles = ul1.children();
					SinaItem opt = null;
					for (int i = 0; eles != null && i < eles.size(); i++) {
						Element ele = eles.get(i);

						// 如果对象为空，则新建item
						opt = Optional.ofNullable(opt).orElse(new SinaItem());
						if (ele.tagName().equalsIgnoreCase("a")) {// 如果是A标签，则寻找上一个textNode的文本

							Node timeNode = ele.previousSibling();// 2016-08-26
																	// 07:30
																	// "\\u00A0"
							// String timeStr =
							// (StringEscapeUtils.unescapeHtml4(timeNode.outerHtml())
							// + "").replaceAll("\\u00A0", " ");// 特殊空格
							String timeStr = StringUtils.replace(timeNode.outerHtml(), "&nbsp;", " ");// 特殊空格
							timeStr = StringUtils.removePattern(timeStr, "^\\s*|\\s*$") + "";
							opt.setNewsTimeStr(timeStr);
							try {// UtilDate.parse("2016-08-26 07:30", 8,
									// "yyyy-MM-dd HH:mm")
								opt.setNewsTime(UtilDate.parse(timeStr, 8, "yyyy-MM-dd HH:mm"));
							} catch (Exception e) {
							}
							opt.setLink(StringUtils.stripToNull(ele.attr("abs:href")));
							opt.setTitle(StringUtils.stripToNull(ele.ownText()));
						} else if (ele.tagName().equalsIgnoreCase("br")) {// 已换行
							result.add(opt);
							opt = null;// 对象清空
						}
					}

				});
		return result;
	}

	public static void main(String[] args) throws IOException {

		String url2 = "http://vip.stock.finance.sina.com.cn/corp/go.php/vCB_AllNewsStock/symbol/sz000002.phtml";
		String urlHK = "http://stock.finance.sina.com.cn/hkstock/news/00701.html";
		String urlUs = "http://biz.finance.sina.com.cn/usstock/usstock_news.php?symbol=GOOG";
		urlUs = "http://biz.finance.sina.com.cn/usstock/usstock_news.php?symbol=JD";
		// Connection connection = Jsoup.connect(url2)
		// Connection connection = Jsoup.connect(urlHK)
		Connection connection = Jsoup.connect("http://tech.sina.com.cn/i/2017-05-08/doc-ifyeycfp9374374.shtml")
				.userAgent(
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
				// .header("x-client-data",
				// "CIq2yQEIpbbJAQjEtskBCP2VygEIwpjKAQjwnMoB")
				.header("x-client-data", RandomStringUtils.randomAlphanumeric(40));
		org.jsoup.nodes.Document doc = connection.timeout(10000).get();
		// List<SinaItem> items = new SinaNewsListUitl().parseNewsList(doc);
		// List<SinaItem> items = new SinaNewsListUitl().parseNewsListHK(doc);
		List<SinaItem> items = new SinaNewsListUitl().parseNewsListUS(doc);
		System.out.println(items);

		NewsHtmlPraseMapper a = new NewsHtmlPraseMapper();
		HtmlBodyPrase findHtmlBodyPrase = a
				.findHtmlBodyPrase("http://tech.sina.com.cn/i/2017-05-08/doc-ifyeycfp9374374.shtml");

		findHtmlBodyPrase.praseNewsInfo(doc,
				new NewsInformation());

	}

	public static class SinaItem {
		private Date newsTime;
		private String newsTimeStr;
		private String title;
		private String link;

		public Date getNewsTime() {
			return newsTime;
		}

		public void setNewsTime(Date newsTime) {
			this.newsTime = newsTime;
		}

		public String getNewsTimeStr() {
			return newsTimeStr;
		}

		public void setNewsTimeStr(String newsTimeStr) {
			this.newsTimeStr = newsTimeStr;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getLink() {
			return link;
		}

		public void setLink(String link) {
			this.link = link;
		}

		@Override
		public String toString() {
			return "SinaItem [newsTime=" + newsTime + ", newsTimeStr=" + newsTimeStr + ", title=" + title + ", link="
					+ link + "]";
		}

	}

}
